var jwt = require('jsonwebtoken');
var jwt_sign = 'system_token'

// 生成Token
var setToken = function (id, userRole, userName) {
  return new Promise((resolve) => {
    const token = jwt.sign({
      id,
      userRole,
      userName
    }, jwt_sign, {
      expiresIn: '24h'
    })
    resolve(`Bearer ${token}`);
  })
}

// 获取Token
var getToken = function (token) {
  return new Promise((resolve, reject) => {
    if(!token) {
      reject("token失效，请重新登录获取");
    }
    const info = jwt.verify(token.split(' ')[1], jwt_sign)
    resolve(info);
  })
}

module.exports = {
  jwt_sign,
  setToken,
  getToken
}